﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EM.Repository.utility;
using System.Data.SqlClient;
using System.Data;

namespace EM.operation.project
{
    public partial class default_edit : System.Web.UI.Page
    {
        string sTask = "";
        public int PROJECTID = 0;
        FillCombo Fc = new FillCombo();
        Conversion oC = new Conversion();
        protected void Page_Load(object sender, EventArgs e)
        {
            Authentication ua = new Authentication();
            ua.CheckRolePageAuthentication(36, Authentication.RedirectionURLTypes.TimeOutPage);

            sTask = Request.QueryString["task"].ToString();
            Int32.TryParse(Request.QueryString["ID"], out PROJECTID);


            if (sTask == "Delete")
            {
                lblSave.Text = "Sil";
            }

            if (!IsPostBack)
            {


                if (sTask == "Insert")
                {
                    Fc.LookUpValue(ddlStatus, "1", true, "PROJECT_STATUS", "TR");

                }

                if (sTask == "Update" || sTask == "Delete")
                {
                    txtProjectCode.Enabled = false;
                    GetData();


                }
            }
        }

        private void GetData()
        {

            DbHelper srv = new DbHelper();
            SqlCommand cmd = new SqlCommand();

            string sSQL = @" SELECT PROJECTID,PROJECTCODE,PROJECTDESC,
                            PROJECTSTATUS FROM TBLPROJECT WHERE PROJECTID = @PROJECTID
                            AND WORKGROUPID = @WORKGROUPID";
            cmd.CommandText = sSQL;
            cmd.Parameters.Add("PROJECTID", SqlDbType.Int).Value = PROJECTID;
            cmd.Parameters.Add("WORKGROUPID", SqlDbType.Int).Value = Session["WORKGROUPID"];
            DataTable dt = srv.GetDataTable(cmd, "");

            if (dt.Rows.Count > 0)
            {
                lblProjectId.Text = dt.Rows[0]["PROJECTID"].ToString();
                txtProjectCode.Text = dt.Rows[0]["PROJECTCODE"].ToString();
                txtProjectDesc.Text = dt.Rows[0]["PROJECTDESC"].ToString();
                Fc.LookUpValue(ddlStatus, dt.Rows[0]["PROJECTSTATUS"].ToString(), true, "PROJECT_STATUS", "TR");
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            DbHelper srv = new DbHelper();
            SqlCommand cmd = new SqlCommand();

            string sSQL = "";

            if (sTask == "Insert")
            {
                sSQL = @" DECLARE @UID INT;
                           INSERT INTO TBLPROJECT
                            (PROJECTCODE,PROJECTDESC,PROJECTSTATUS,CREUSERID,CREDATE, WORKGROUPID)
                            SELECT @PROJECTCODE,@PROJECTDESC,@PROJECTSTATUS,@CREUSERID,GETDATE(), @WORKGROUPID
                          SET @UID = SCOPE_IDENTITY();

                            SELECT @UID;";

            }
            else if (sTask == "Update")
            {
                sSQL = @"UPDATE TBLPROJECT SET
                        PROJECTDESC = @PROJECTDESC,
                        PROJECTSTATUS = @PROJECTSTATUS,
                        MODUSERID = @CREUSERID,
                        MODDATE = GETDATE()
                        WHERE PROJECTID = @PROJECTID";
            }
            else if (sTask == "Delete")
            {
                sSQL = @"DELETE FROM TBLPROJECT  WHERE PROJECTID = @PROJECTID";
            }

            
            cmd.Parameters.AddWithValue("@WORKGROUPID", Session["WORKGROUPID"]);
            cmd.Parameters.AddWithValue("@PROJECTDESC", txtProjectDesc.Text.Trim());
            cmd.Parameters.AddWithValue("@PROJECTCODE", txtProjectCode.Text.Trim());
            cmd.Parameters.AddWithValue("@PROJECTSTATUS", ddlStatus.SelectedValue);
            cmd.Parameters.AddWithValue("@CREUSERID", Session["USERID"]);
            cmd.Parameters.AddWithValue("@PROJECTID", this.PROJECTID);

            cmd.CommandText = sSQL;
            
            DataTable dt = srv.GetDataTable(cmd, "");


            string strScript = "window.opener['rgMaster'].AjaxRequest('rgMaster', 'Rebind'); window.close();";
            ClientScript.RegisterStartupScript(typeof(string), "", strScript.ToString(), true);
        }
    }
}